package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.DzZiyuanMapper;
import com.ruoyi.system.domain.DzZiyuan;
import com.ruoyi.system.service.IDzZiyuanService;


import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.mapper.DzHuiyuanMapper;
import com.ruoyi.system.domain.DzHuiyuan;
import com.ruoyi.system.service.IDzHuiyuanService;

/**
 * 资源Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-09-02
 */
@Service
public class DzZiyuanServiceImpl implements IDzZiyuanService 
{
    @Autowired
    private DzZiyuanMapper dzZiyuanMapper;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private IDzHuiyuanService dzHuiyuanService;

    /**
     * 查询资源
     * 
     * @param zyId 资源主键
     * @return 资源
     */
    @Override
    public DzZiyuan selectDzZiyuanByZyId(Long zyId)
    {
        return dzZiyuanMapper.selectDzZiyuanByZyId(zyId);
    }

    /**
     * 查询资源列表
     * 
     * @param dzZiyuan 资源
     * @return 资源
     */
    @Override
    public List<DzZiyuan> selectDzZiyuanList(DzZiyuan dzZiyuan)
    {

        //权限控制
        SysUser user=  SecurityUtils.getLoginUser().getUser();
        List<SysRole> listRole= sysRoleService.selectRolesByUserId(user.getUserId());
        int datascope = 5;
        for(SysRole role:listRole){
            if( role.getDataScope()!=null&&Integer.parseInt(role.getDataScope())<datascope){
                datascope=Integer.parseInt(role.getDataScope());
            }
        }
        dzZiyuan.setDatascope(datascope);
        if(datascope==4){
            dzZiyuan.setCheckDeptId(user.getDeptId());
        }
        if(datascope==5){
            DzHuiyuan huiyuan =  dzHuiyuanService.selectDzHuiyuanByDn(user.getPhonenumber());
            if(huiyuan!=null){
                dzZiyuan.setCheckHuiyuanId(huiyuan.getId());
            }else{
                dzZiyuan.setCheckHuiyuanId(0l);
            }

        }
        return dzZiyuanMapper.selectDzZiyuanList(dzZiyuan);
    }

    @Override
    public List<DzZiyuan> selectDzZiyuanListWeb(DzZiyuan dzZiyuan)
    {
        return dzZiyuanMapper.selectDzZiyuanList(dzZiyuan);
    }

    /**
     * 新增资源
     * 
     * @param dzZiyuan 资源
     * @return 结果
     */
    @Override
    public int insertDzZiyuan(DzZiyuan dzZiyuan)
    {
        dzZiyuan.setCreateTime(DateUtils.getNowDate());
        return dzZiyuanMapper.insertDzZiyuan(dzZiyuan);
    }

    /**
     * 修改资源
     * 
     * @param dzZiyuan 资源
     * @return 结果
     */
    @Override
    public int updateDzZiyuan(DzZiyuan dzZiyuan)
    {
        return dzZiyuanMapper.updateDzZiyuan(dzZiyuan);
    }

    /**
     * 批量删除资源
     * 
     * @param zyIds 需要删除的资源主键
     * @return 结果
     */
    @Override
    public int deleteDzZiyuanByZyIds(Long[] zyIds)
    {
        return dzZiyuanMapper.deleteDzZiyuanByZyIds(zyIds);
    }

    /**
     * 删除资源信息
     * 
     * @param zyId 资源主键
     * @return 结果
     */
    @Override
    public int deleteDzZiyuanByZyId(Long zyId)
    {
        return dzZiyuanMapper.deleteDzZiyuanByZyId(zyId);
    }
}
